/*
 * @Description:
 * @Author:Huang Xin Ming
 * @Date:2024-11-11 15:53:03
 * @LastEditors:Huang Xin Ming
 * @LastEditTime:2024-11-12 08:55:27
 */
import { defineStore } from 'pinia'
import { login } from '@/api/login'
import { getInfo } from '@/api/user'
import type { LoginReqInterface } from '@/api/types/login'
import type { UserInfoInterface } from '@/api/types/user'
import { useUserStore } from '@/store/modules/user'

export const useLoginStore = defineStore('login',() => {
    /**
     * @Description: 登录
     * @return {*}
     * @author:Huang Xin Ming
     */    
    const Login = async (data:LoginReqInterface) => {
        const { setToken,setUserInfo } = useUserStore()
        // 登录成功获取token信息
        const { token,msg } = await login(data)
        // 储存token
        setToken(token)
        // 获取用户信息
        const userInfo:UserInfoInterface = await getInfo()
        // 储存用户信息
        setUserInfo(userInfo)
    }
    /**
     * @Description: 微信登录
     * @return {*}
     * @author:Huang Xin Ming
     */    
    const WXLogin = (timeout: number) => {
        return new Promise((resolve,reject) => {
            wx.login({
                timeout,
                success(res:any) {
                    resolve(res)
                },
                fail(err:any){
                    reject(err)
                }
            })
        })
    }
    return {
        Login,
        WXLogin
    }
})